﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace Ants.Utilities
{
    public static class Debug
    {
        private static StringBuilder stringBuilder = new StringBuilder(50);

        private const string COLOR_TAG_TAIL = "</color>";
        public static void PrintParams(params object[] infos)
        {
            stringBuilder.Clear();
            stringBuilder.Append("[ ");
            foreach (var info in infos)
            {
                stringBuilder.Append(info + " ");
            }

            stringBuilder.Append("]");
            UnityEngine.Debug.Log(stringBuilder.ToString());
        }

        public static void PrintCollection<T>(IEnumerable<T> collection)
        {
            stringBuilder.Clear();
            stringBuilder.Append("[ ");
            foreach (var e in collection)
            {
                stringBuilder.Append(e + " ");
            }

            stringBuilder.Append("]");
            UnityEngine.Debug.Log(stringBuilder.ToString());
        }

        public static void PrintError(object info)
        {
            stringBuilder.Clear();
            stringBuilder.Append("<color=red>Error : ");
            stringBuilder.Append(info);
            stringBuilder.Append(COLOR_TAG_TAIL);
            UnityEngine.Debug.Log(stringBuilder.ToString());
        }

        public static void PrintHint(object info)
        {
            stringBuilder.Clear();
            stringBuilder.Append("<color=#66ccff>Hint : ");
            stringBuilder.Append(info);
            stringBuilder.Append(COLOR_TAG_TAIL);
            UnityEngine.Debug.Log(stringBuilder.ToString());
        }

        public static void PrintWarning(object info)
        {
            stringBuilder.Clear();
            stringBuilder.Append("<color=yellow>Warning : ");
            stringBuilder.Append(info);
            stringBuilder.Append(COLOR_TAG_TAIL);
            UnityEngine.Debug.Log(stringBuilder.ToString());
        }

        public static void PrintDataClass<T>(T dataObj) where T : new()
        {
            UnityEngine.Debug.Log(DataClassToString(dataObj));
            
        }
        private static string DataClassToString<T>(T data)
        {
            stringBuilder.Clear();
            var t = typeof(T);
            string colorHead="<color=yellow>";
            string colorTail = COLOR_TAG_TAIL;
            stringBuilder.Append(colorHead+ "Class : ");
            stringBuilder.Append(t.Name+colorTail+ "\n");
            foreach (var fieldInfo in t.GetFields())
            {
                stringBuilder.Append(colorHead+fieldInfo.FieldType.Name+"  "+fieldInfo.Name+" = ");
                stringBuilder.Append(fieldInfo.GetValue(data)+colorTail+"\n");
            }
            
            return stringBuilder.ToString();
        }

    }
    
    
    
    
    
    
    
    
    
    

    public class Painter
    {
        public static Color color = Color.white;

        public static void DrawGridOnXZ(Vector3 original, float rightWidth = 1f, float upHeight = 1f,
            float leftWidth = 0, float downHeight = 0, float duration = 0)
        {
            Vector3 vfrom, vto;
            vfrom = new Vector3(-leftWidth, 0, -downHeight) + original;
            vto = vfrom;
            vto.z += upHeight + downHeight;
            UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
            vfrom = new Vector3(rightWidth, 0, -downHeight) + original;
            vto = vfrom;
            vto.z += upHeight + downHeight;
            UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);

            vfrom = new Vector3(-leftWidth, 0, -downHeight) + original;
            vto = vfrom;
            vto.x += leftWidth + rightWidth;
            UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
            vfrom = new Vector3(-leftWidth, 0, upHeight) + original;
            vto = vfrom;
            vto.x += leftWidth + rightWidth;
            UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
        }

        public static void DrawCellSpaceOnXZ(Vector3 original, Vector2 cellSize, Vector2 spaceSize,
            bool flipDrawByX = false, float duration = 0)
        {
            Vector2Int cellNum = Vector2Int.zero;
            cellNum.y = (int) (spaceSize.y / cellSize.y);
            cellNum.x = (int) (spaceSize.x / cellSize.x);
            DrawCellSpaceOnXZ(original, cellSize, cellNum, flipDrawByX, duration);
        }

        public static void DrawCellSpaceOnXZ(Vector3 original, Vector2 cellSize, Vector2Int cellNum,
            bool flipDrawByX = false, float duration = 0)
        {
            float spaceHeight = cellNum.y * cellSize.y;
            float spaceWidth = cellNum.x * cellSize.x;
            Vector3 vfrom, vto;
            int xFlip = flipDrawByX ? 1 : -1;
            for (int i = 0; i <= cellNum.x; i++)
            {
                vfrom = new Vector3(i * cellSize.x, 0, 0) + original;
                vto = vfrom;
                vto.z += spaceHeight * xFlip;
                UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
            }

            for (int j = 0; j <= cellNum.y; j++)
            {
                vfrom = new Vector3(0, 0, j * cellSize.y * xFlip) + original;
                vto = vfrom;
                vto.x += spaceWidth;
                UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
            }
        }


        public static void DrawPath(Vector3 original, Vector3[] points, float duration = 0)
        {
            Vector3 vfrom, vto;
            for (int i = 0; i < points.Length - 1; i++)
            {
                vfrom = points[i] + original;
                vto = points[i + 1] + original;
                UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
            }
        }
        public static void DrawPath(Vector2 original, Vector2[] points, float duration = 0)
        {
            Vector2 vfrom, vto;
            for (int i = 0; i < points.Length - 1; i++)
            {
                vfrom = points[i] + original;
                vto = points[i + 1] + original;
                UnityEngine.Debug.DrawLine(vfrom, vto, color, duration);
            }
        }
    }

}